package qlsl.androiddesign.view.subview.activityview;

import java.util.ArrayList;
import java.util.List;

import com.qlsl.androiddesign.kuaiwa.R;

import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import qlsl.androiddesign.activity.subactivity.BusinessOrderActivity;
import qlsl.androiddesign.adapter.baseadapter.FragmentPagerAdapter;
import qlsl.androiddesign.fragment.basefragment.BaseFragment;
import qlsl.androiddesign.fragment.subfragment.OrderBuyFragment;
import qlsl.androiddesign.fragment.subfragment.OrderSellFragment;
import qlsl.androiddesign.listener.GuidePageChangeListener;
import qlsl.androiddesign.view.baseview.FunctionView;

/**
 * 我的订单页[商家]<br/>
 * 需要传入的键：<br/>
 * 传入的值类型： <br/>
 * 传入的值含义：<br/>
 * 是否必传 ：
 */
public class BusinessOrderView extends FunctionView<BusinessOrderActivity> {

	private TextView[] tv_guides;

	private ImageView iv_cursor;

	private ViewPager viewPager;

	private List<BaseFragment> list;

	private int preIndex;

	public BusinessOrderView(BusinessOrderActivity activity) {
		super(activity);
		setContentView(R.layout.activity_business_order);
	}

	protected void initView(View view) {
		setTitle("我的订单");
		tv_guides = new TextView[2];
		tv_guides[0] = (TextView) view.findViewById(R.id.tv_guide1);
		tv_guides[1] = (TextView) view.findViewById(R.id.tv_guide2);
		iv_cursor = (ImageView) view.findViewById(R.id.iv_cursor);
		viewPager = (ViewPager) view.findViewById(R.id.viewPager);
		viewPager.setOffscreenPageLimit(2);
		setCursorImageMatrix();
	}

	protected void initData() {

	}

	protected void initListener() {

	}

	public <T> void showData(T... t) {

	}

	public <T> void showNoData(T... t) {

	}

	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.tv_guide1:
			doClickGuideView(view);
			break;
		case R.id.tv_guide2:
			doClickGuideView(view);
			break;
		default:
			int index = viewPager.getCurrentItem();
			list.get(index).onClick(view);
			break;
		}
	}

	private void setCursorImageMatrix() {
		int bmpW = BitmapFactory.decodeResource(activity.getResources(), R.drawable.iv_cursor).getWidth();
		DisplayMetrics dm = new DisplayMetrics();
		activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
		int screenW = dm.widthPixels;
		int offset = (screenW / tv_guides.length - bmpW) / 2;
		Matrix matrix = new Matrix();
		matrix.postTranslate(offset, 0);
		iv_cursor.setImageMatrix(matrix);

		setViewPagerAdapter(offset, bmpW);
	}

	private void setViewPagerAdapter(int offset, int bmpW) {
		list = new ArrayList<BaseFragment>();
		BaseFragment fragment1 = new OrderBuyFragment();
		BaseFragment fragment2 = new OrderSellFragment();
		list.add(fragment1);
		list.add(fragment2);
		viewPager.setAdapter(new FragmentPagerAdapter(activity.getSupportFragmentManager(), list));
		viewPager.setCurrentItem(0);
		tv_guides[0].setActivated(true);
		viewPager.setOnPageChangeListener(new GuidePageChangeListener(tv_guides, iv_cursor, offset, bmpW));
	}

	private void doClickGuideView(View view) {
		for (int index = 0; index < tv_guides.length; index++) {
			if (view == tv_guides[index]) {
				viewPager.setCurrentItem(index);
				tv_guides[index].setActivated(true);
			} else {
				tv_guides[index].setActivated(false);
			}
		}
	}

}
